package im.starchat.chat.activity.profile;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;
import im.starchat.chat.R;
import im.starchat.chat.XLApplication;
import im.starchat.chat.activity.BaseActivity;
import im.starchat.chat.rest.model.AccountBean;
import im.starchat.chat.rest.model.WeixinUserBean;
import im.starchat.chat.rest.model.api.NullResponse;
import im.starchat.chat.utils.RetrofitErrorUtil;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;


/**
 * 提现,我的收益
 */
public class WithdrawActivity extends BaseActivity implements View.OnClickListener{

    @InjectView(R.id.withdraw_back)
    ImageView mBackBtn;
    @InjectView(R.id.popularity)
    TextView mPopularity;

    @InjectView(R.id.withdraw_total)
    TextView mWithDrawTotal;
    @InjectView(R.id.withdraw_today)
    TextView mWithDrawToday;

    @InjectView(R.id.withdraw)
    Button mWithDrawBtn;

    AccountBean mAccountBean;

    UMShareAPI mShareAPI;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_withdraw);
        ButterKnife.inject(this);
        mBackBtn.setOnClickListener(this);
        mWithDrawBtn.setOnClickListener(this);
        mShareAPI = UMShareAPI.get(this);
        getAccountInfo();
    }

    @Override
    protected void onResume() {
        if(mBackBtn != null){
            getAccountInfo();
        }
        super.onResume();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.withdraw_back:
                finish();
                break;
            case R.id.withdraw:
                //TODO 提现
                if(mAccountBean != null){
                    if(mAccountBean.getMax_withdraw_money() > 0.0f){
                        if(!TextUtils.isEmpty(mAccountBean.getRecipient())){
                                withDraw(mAccountBean.getMax_withdraw_money() + "");
                        }else{
                            toast("请先绑定微信", Toast.LENGTH_SHORT);
                            SHARE_MEDIA platform = SHARE_MEDIA.WEIXIN;
                            mShareAPI.doOauthVerify(this, platform, umAuthListener);
                        }
                    }else{
                        toast("可提现金额不足", Toast.LENGTH_SHORT);
                    }


                }
//                finish();
                break;
        }
    }

    private UMAuthListener umAuthListener = new UMAuthListener() {
        @Override
        public void onComplete(SHARE_MEDIA platform, int action, final Map<String, String> data) {
            if(platform == SHARE_MEDIA.WEIXIN){

                XLApplication.getInstance().getRestClient().getWeiXinUserInfo(data.get("access_token"), data.get("openid"), new Callback<WeixinUserBean>() {
                    @Override
                    public void success(WeixinUserBean weixinUserBean, Response response) {
                        XLApplication.getInstance().getRestClient().bindWeixin(weixinUserBean.getOpenid(), new Callback<NullResponse>() {
                            @Override
                            public void success(NullResponse nullResponse, Response response) {
                                withDraw(mAccountBean.getMax_withdraw_money() + "");
                            }

                            @Override
                            public void failure(RetrofitError error) {

                            }
                        });

                    }

                    @Override
                    public void failure(RetrofitError error) {
                        toast("登录失败", Toast.LENGTH_SHORT);
                        dismissProgressDialog();
                    }
                });
            }
        }

        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
        }

        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
        }
    };

    private void getAccountInfo(){
        showProgressDialog("获取账户信息");
        XLApplication.getInstance().getRestClient().getAccountInfo(new Callback<AccountBean>() {
            @Override
            public void success(AccountBean accountBean, Response response) {
                dismissProgressDialog();
                if (accountBean != null) {
                    mAccountBean = accountBean;
                    mPopularity.setText(accountBean.getTotal_popularity() + "");
                    mWithDrawTotal.setText("￥" + accountBean.getWithdraw_money() + "");
                    mWithDrawToday.setText("￥" + accountBean.getMax_withdraw_money() + "");
//                    if (accountBean.getProducts() != null) {
//                        mMyDiamondAdapter.refreshProductList(configResponse.getProducts());
//                    }
                }
            }

            @Override
            public void failure(RetrofitError error) {
                dismissProgressDialog();
            }
        });
    }

    private void withDraw(String amount){
        showProgressDialog("正在提现");
        XLApplication.getInstance().getRestClient().withDraw(amount, new Callback<NullResponse>() {
            @Override
            public void success(NullResponse nullResponse, Response response) {

                if(RetrofitErrorUtil.handleResponse(WithdrawActivity.this, response)){
                    dismissProgressDialog();
                    return;
                }
                dismissProgressDialog();
                toast("提现成功", Toast.LENGTH_SHORT);
                getAccountInfo();
            }

            @Override
            public void failure(RetrofitError error) {
                RetrofitErrorUtil.handleError(WithdrawActivity.this, error);
                toast("提现失败", Toast.LENGTH_SHORT);
                dismissProgressDialog();
            }
        });
    }

}
